import 'package:bike_ride/common/util/hysize_fit.dart';
import 'package:flutter/material.dart';
import 'package:flutter_easyloading/flutter_easyloading.dart';

class XbButton extends StatelessWidget {
  final double? width;
  final double? height;
  final EdgeInsets? padding;
  final bool isDisabled;
  final bool isDefault;
  final bool loading;
  final bool isOutlined;
  final bool hasBorder;
  final Widget? child;
  final Color? color;
  final BorderRadius? borderRadius;
  final List<BoxShadow>? boxShadow;
  final GestureTapCallback? onPressed;
  final String? disabledTips;
  final bool isDisabledCanClick;
  final Color? borderColor;

  const XbButton({
    Key? key,
    this.width,
    this.height,
    this.isDisabled = false,
    this.loading = false,
    this.child,
    this.borderRadius,
    this.boxShadow,
    this.color,
    this.isDefault = false,
    this.onPressed,
    this.isOutlined = false,
    this.padding,
    this.disabledTips,
    this.isDisabledCanClick = false,
    this.hasBorder = true,
    this.borderColor
  }) : super(key: key);

  @override
  Widget build(BuildContext context) {
    final themePrimary = borderColor??Theme.of(context).colorScheme.primary;

    Color? buttonPrimary = themePrimary;
    Color textColor = Colors.white;

    if (color != null) {
      buttonPrimary = color;
    }

    if (isDefault) {
      buttonPrimary = Colors.white;
      textColor = themePrimary;
    }

    if (isDisabled) {
      if (color != null) {
        buttonPrimary = color!.withOpacity(0.5);
      } else {
        buttonPrimary = themePrimary.withOpacity(0.5);
      }
    }

    if (isOutlined) {
      return OutlinedButton(
        style: ButtonStyle(
          padding: MaterialStateProperty.all(padding ?? EdgeInsets.all(10.rpx)),
          minimumSize: MaterialStateProperty.all(
              Size(width ?? 210.rpx, height ?? 72.rpx)),
          foregroundColor: MaterialStateProperty.all(
              isDisabled ? Colors.black.withOpacity(0.25) : buttonPrimary),
          backgroundColor: MaterialStateProperty.resolveWith((states) {
            //设置按下时的背景颜色
            if (states.contains(MaterialState.pressed)) {
              return isDisabled
                  ? Colors.black.withOpacity(0.03)
                  : buttonPrimary!.withOpacity(0.1);
            }
            //默认不使用背景颜色
            return isDisabled
                ? Colors.black.withOpacity(0.05)
                : buttonPrimary!.withOpacity(0.05);
          }),
          overlayColor: MaterialStateProperty.all(isDisabled
              ? Colors.black.withOpacity(0.03)
              : buttonPrimary!.withOpacity(0.3)),
          side: MaterialStateProperty.all(
            BorderSide(
              color:
                  isDisabled ? Colors.black.withOpacity(0.03) : buttonPrimary!,
              width: 1.rpx,
            ),
          ),
          shape: MaterialStateProperty.all(RoundedRectangleBorder(
              borderRadius: borderRadius ?? BorderRadius.circular(8.rpx))),
        ),
        onPressed: () {
          if (((isDisabledCanClick && isDisabled) || !isDisabled) &&
              onPressed != null) {
            onPressed!();
          } else if (isDisabled && disabledTips != null) {
            EasyLoading.showToast(disabledTips ?? '禁用中',toastPosition: EasyLoadingToastPosition.bottom);
          }
        },
        child: DefaultTextStyle(
          style: TextStyle(
            color: isDisabled ? Colors.black.withOpacity(0.25) : buttonPrimary,
            fontSize: 32.rpx,
            fontWeight: FontWeight.w400,
          ),
          child: child!,
        ),
      );
    }

    return ElevatedButton(
      style: ButtonStyle(
        padding: MaterialStateProperty.all(padding ?? EdgeInsets.all(10.rpx)),
        minimumSize:
            MaterialStateProperty.all(Size(width ?? 200.rpx, height ?? 96.rpx)),
        elevation: MaterialStateProperty.all(0),
        backgroundColor: MaterialStateProperty.all(buttonPrimary),
        foregroundColor: MaterialStateProperty.all(textColor),
        overlayColor: MaterialStateProperty.resolveWith((states) {
          //设置按下时的背景颜色
          if (states.contains(MaterialState.pressed)) {
            return isDefault
                ? themePrimary.withOpacity(0.15)
                : Colors.white.withOpacity(0.15);
          }
          //默认不使用背景颜色
          return buttonPrimary;
        }),
        side: MaterialStateProperty.all(
          hasBorder
              ? BorderSide(
                  color: isDefault
                      ? themePrimary
                      : isDisabled
                          ? Colors.transparent
                          : buttonPrimary!,
                  width: 2.rpx,
                )
              : BorderSide.none,
        ),
        shape: MaterialStateProperty.all(RoundedRectangleBorder(
            borderRadius: borderRadius ?? BorderRadius.circular(8.rpx))),
      ),
      onPressed: () {
        if (((isDisabledCanClick && isDisabled) || !isDisabled) &&
            onPressed != null) {
          onPressed!();
        } else if (isDisabled && disabledTips != null) {
          EasyLoading.showToast(disabledTips ?? '禁用中',toastPosition: EasyLoadingToastPosition.bottom);
        }
      },
      child: DefaultTextStyle(
        style: TextStyle(
          color: textColor,
          fontSize: 32.rpx,
          fontWeight: FontWeight.w400,
        ),
        child: child!,
      ),
    );
  }
}
